<template>
  <div class="main">
    <el-timeline :reverse="true">
      <div
        v-for="item of deviceFaultRecordList"
        :key="item.recordId"
        class="timeline"
      >
        <el-timeline-item
          class="item"
          :timestamp="item.occurTime"
          placement="top"
        >
          <el-badge
            is-dot
            :hidden="item.read"
            class="message-card"
          >
            <el-card>
              <el-tag
                v-if="item.processed"
                type="info"
                style="margin-right: 20px"
              >
                已处理
              </el-tag>
              <el-tag
                v-if="item.Read === 1"
                type="info"
              >
                已读
              </el-tag>
              <h4>{{ item.faultType }}</h4>
              <p>地点：{{ item.locale }}</p>
              <p>设备编号：{{ item.deviceId }}</p>
              <p>异常情况：{{ item.faultInfo }}</p>
              <div class="message-card-footer">
                <div v-if="!item.read">
                  <el-button
                    size="small"
                    @click="handleRead(item)"
                  >
                    已读
                  </el-button>
                </div>
              </div>
            </el-card>
          </el-badge>

        </el-timeline-item>
      </div>

    </el-timeline>
  </div>
</template>

<script>
import { listDeviceFaultRecords, readFaultRecord } from '@/api/device-fault-record'
import { parseTime } from '@/utils'
import { mapGetters } from 'vuex'

export default {

  data() {
    return {
      queryParams: {
        pageSize: 5,
        pageNum: 1,
        userId: '',
        orderBy: 'occur_time desc'
      },
      faultRecordReadDTO: {
        userId: 1,
        faultRecordId: '',
        read: true,
        readTime: ''
      },
      deviceFaultRecordList: []
    }
  },
  mounted() {
    this.fetchDeviceFaultRecordList()
  },
  computed: {
    ...mapGetters(['userId'])
  },
  created() {
    this.queryParams.userId = this.userId
  },
  methods: {

    fetchDeviceFaultRecordList() {
      listDeviceFaultRecords(this.queryParams).then(response => {
        this.deviceFaultRecordList = response.data
      })
    },
    handleRead(record) {
      const faultRecordReadDTO = {
        userId: this.userId,
        faultRecordId: record.recordId,
        read: true,
        readTime: parseTime(new Date())
      }
      readFaultRecord(faultRecordReadDTO).then(response => {
        this.fetchDeviceFaultRecordList()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.item {
  margin-top: 10px;
  margin-right: 40px;
}
.message-card {
  width: 100%;
}
.message-card:hover {
  /*background-color: #F9F9F9;
  cursor:pointer*/
}
.message-card-footer {

}
</style>
